<?php
defined('IN_SYS') or die ('Restricted Access');
class SysMinify {
	public $output = 'file';	// file|content
	public $files = array();
	
	function compress ($file) {
		$info = pathinfo_file($file);
            
        $buffer = $this->getContentFile($file);
        if ($info['extension'] == 'css') {
        	$buffer = $this->compress_css($buffer);
        } else if ($info['extension'] == 'js') {
        	$buffer = $this->compress_js($buffer);
        }
        
        return $buffer;
	}
	
	function compress_js($buffer) {
        /* remove comments */
        $buffer = preg_replace("/((?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:\/\/.*))/", "", $buffer);
        /* remove tabs, spaces, newlines, etc. */
        $buffer = str_replace(array("\r\n","\r","\t","\n",'  ','    ','     '), '', $buffer);
        /* remove other spaces before/after ) */
        $buffer = preg_replace(array('(( )+\))','(\)( )+)'), ')', $buffer);
        
        return $buffer.";";
    }
    
    function compress_css($buffer) {
        /* remove comments */
        $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
        /* remove tabs, spaces, newlines, etc. */
        $buffer = str_replace(array("\r\n","\r","\n","\t",'  ','    ','     '), '', $buffer);
        /* remove other spaces before/after ; */
        $buffer = preg_replace(array('(( )+{)','({( )+)'), '{', $buffer);
        $buffer = preg_replace(array('(( )+})','(}( )+)','(;( )*})'), '}', $buffer);
        $buffer = preg_replace(array('(;( )+)','(( )+;)'), ';', $buffer);
        return $buffer;
    }
    
    function compress_html($buffer) {
		$search = array(
			'/\n/',				// replace end of line by a space
			'/\>[^\S ]+/s',		// strip whitespaces after tags, except space
			'/[^\S ]+\</s',		// strip whitespaces before tags, except space
			'/(\s)+/s'			// shorten multiple whitespace sequences
		);
		$replace = array(
			' ',
			'>',
			' <',
			'\\1'
		);
		$buffer = preg_replace($search, $replace, $buffer);
		return $buffer;
    }
    
    
    function buildFile($file='', $compress=true, $cache=true) {
    	if (!$compress) {
    		return $file;
    	}
    	
    	if (strpos($file,'.min.')!==false || $cache == false) {
    		return $file;
    	}
    	
    	if(strpos(basename($file),'.min.')===false) { //compress files that aren't minified
            $info = @pathinfo_file($file);
            
            if (!isset($info['filename'])) return $file;
            
            $file_new = $info['dirname'] . DS . $info['filename'] . '.min.' . $info['extension'];
            $buffer = $this->getContentFile($file);
            if ($info['extension'] == 'css') {
            	$buffer = $this->compress_css($buffer);
            } else if ($info['extension'] == 'js') {
            	$buffer = $this->compress_js($buffer);
            } else if ($info['extension'] == 'html' || $info['extension'] == 'htm' || $info['extension'] == 'tpl') {
            	$buffer = $this->compress_html($buffer);
            }
            
            // open new file
            $handle = @fopen($file_new,'w+');
			if ($handle) {
				fwrite($handle,$buffer,strlen($buffer)); 
				fclose($handle);
			}
			if (file_exists($file_new)) {
				$file = $file_new;
			}
        }
        
        return $file;
    }
    
    /**
	 * return content of file
	 */
	function getContentFile ($path='') {
		$content = @file_get_contents($path);
		return $content;
	}
}